<?php

class Application_Model_DocumentsMapper
{
 protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Documents');
        }
        return $this->_dbTable;
    }

    public function add(Application_Model_Documents $documents)
    {
    try{
      $data = array(
          'DOC_NAME'=>$documents->getDocName(),
          'FILE_NAME'=>$documents->getFileName(),
          'FILE_SIZE'=>$documents->getFileSize(),
          'CATEGORY'=>$documents->getCategory(),
          'CREATED_DATE'=>$documents->getCreatedDate(),
          'CREATED_BY'=>$documents->getCreatedBy(),
          'YEAR'=>$documents->getYear()
          );

      $this->getDbTable()->insert($data);

    }catch(Zend_Db_Statement_Exception $e){
        Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
          throw new Exception("Technical difficulties experienced in adding Documents");
    }
 }
 
     public function update(Application_Model_Documents $documents)
    {
    try{
       $data = array(
          'DOC_NAME'=>$documents->getDocName(),
          'CATEGORY'=>$documents->getCategory(),
          'UPDATED_DATE'=>$documents->getUpdatedDate(),
          'UPDATED_BY'=>$documents->getUpdatedBy(),
          'YEAR'=>$documents->getYear()
          );

      $this->getDbTable()->update($data, array('DOC_ID = ?' => $documents->getDocId()));
      
    }catch(Zend_Db_Statement_Exception $e){
        Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
          throw new Exception("Technical difficulties experienced in updating Documents");
    }
 }

  public function get(Application_Model_Documents $documents)
    {
         try{
              $select =  $this->getDbTable()->select();

            if(trim($documents->getDocId()) !="" && $documents->getDocId() !=0 ){
                $select->where('DOC_ID =?', $documents->getDocId());
            }
              
             if(trim($documents->getDocName()) !=""){
                $select->where('DOC_NAME like ?','%'. $documents->getDocName().'%');
            }

             if(trim($documents->getCategory()) !=""){
                $select->where('CATEGORY like ?','%'. $documents->getCategory().'%');
            }
             if(trim($documents->getYear()) !="" &&  $documents->getYear() != 0){
                $select->where('YEAR = ?',$documents->getYear());
            }

             if(trim($documents->getSortBy()) !=""){
                $select->order($documents->getSortBy());
            }else{
                $select->order('CREATED_DATE DESC');
            }

       //     Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($select->__toString());
             $rows =  $this->getDbTable()->fetchAll($select);

             return $rows;

         }catch(Zend_Db_Statement_Exception $e){
             Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
             throw new Exception("Technical difficulties experienced while fetching Documents");
          }

    }

    public function delete($id){
         try{

           $where=  $this->getDbTable()->getAdapter()->quoteInto('DOC_ID=?', $id);

          $this->getDbTable()->delete($where);


         }catch(Zend_Db_Statement_Exception $e){
             Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
             throw new Exception("Technical difficulties experienced in deleting documents");
          }
    }

}

